<?php
declare (strict_types=1);

namespace app\exam\logic\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\exam\model\admin\PaperCategory as CategoryModel;

/**
 * 试卷分类逻辑层
 */
class PaperCategory extends CategoryModel
{
    /**
     * 分类数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @param int $pid
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($pid = 0)
    {
        return $this->select();
    }

    /**
     * 创建分类
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @param array $data
     * @return bool
     */
    public function createCategory(array $data)
    {
        // 验证数据
        $this->getValidate()->scene('add')->failException()->check($data);

        if (!$ret = $this->save($data)) {
            throw new LogicException('新建失败');
        }

        Log::write('新建试卷分类【' . $data['title'] . '】', 'exam', 'create', true);

        return $ret;
    }

    /**
     * 编辑分类
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @param array $data
     * @return array|\think\Model
     */
    public function editCategory(int $id, array $data)
    {

        $that = $this->findOrEmpty($id);
        if ($that->isEmpty()) {
            throw new LogicException('分类不存在');
        }

        // 验证数据
        $this->getValidate()->scene('edit')->failException()->check($data);

        $that->title = $data['title'];
        if (!$that->save()) {
            throw new LogicException('更新失败');
        }

        Log::write('更新试卷分类【' . $data['title'] . '】', 'exam', 'update', true);

        return $that;
    }

    /**
     * 删除分类
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @param $id
     * @return bool
     */
    public function deleteCategory(int $id)
    {
        $category = $this->findOrEmpty($id);
        if ($category->isEmpty()) {
            throw new LogicException('分类不存在');
        }
        // 是否有子级
        if ($category->getAttr('num') > 0) {
            throw new LogicException('分类下存在试卷，不可删除');
        }
        if (!$category->delete()) {
            throw new LogicException("删除失败");
        }

        Log::write('删除试卷分类【' . $category->title . '】', 'exam', 'delete', true);
    }

    /**
     * 无限极分类
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/29
     *
     * @param int $pid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cascader(?int $mhmId = null)
    {
        if ($mhmId) {
            $cate = $this->withoutGlobalScope(['mhm'])->where('mhm_id', $mhmId)->field('id as value,title')->select();
        } else {
            $cate = $this->field('id as value,title')->select();
        }

        return $cate->toArray();
    }

    /**
     * 解析题库名称
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/24
     *
     * @param $ids
     * @return string
     */
    public function parseTitle($ids)
    {
        $ids   = is_array($ids) ? $ids : explode(',', (string)$ids);
        $cates = $this->withoutGlobalScope(['mhm'])->whereIn('id', $ids)->column('title');

        return implode('/', $cates);
    }

}
